<?php

namespace app\admin\controller;

use app\admin\model\RoomModel;
use app\admin\model\SubjectModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;

class SubjectController extends AdminBaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $name = input('name');
        $subject = Db::name('subject')
            ->join('cmf_teacher','cmf_teacher.id=cmf_subject.tid')
            ->where(function (Query $query)use ($name){
                if ($name){
                    $query->where('cmf_subject.name','like',"%$name%");
                }
            })
            ->field('cmf_subject.*,cmf_teacher.name as teacher_name')
            ->order('id DESC')
            ->paginate(3,false,['query' => $this->request->param()]);
        $page = $subject->render();
        return view('index',['page'=>$page,'subject'=>$subject]);

    }

    public function add()
    {
        if (request()->isPOst()){
            $add = Db::name('subject')->insert(input());
            if (empty(input('name'))){
                $this->error('学科不能为空');
            }
            if ($add){
                $this->success('添加成功', 'subject/index');
            } else {
                $this->error('添加失败');
            }
            }
        $teacher = Db::name('teacher')->all();
        return view('add',['teacher'=>$teacher]);
        }



    public function edit($id)
    {
        if (request()->isPOst()){
            $edit = Db::name('subject')->update(input());
            if ($edit){
                $this->success('修改成功', 'subject/index');
            } else {
                $this->error('修改失败');
            }
        }
        $teacher = Db::name('teacher')->select();
        $subject = Db::name('subject')->get($id);
        return view('edit',['teacher'=>$teacher,'subject'=>$subject]);
    }


    public function delete($id)
    {
        //
        SubjectModel::destroy($id);
    }
    public function delAll()
    {
        $ids = input('ids/a');
        $del = SubjectModel::destroy($ids);
        if ($del){
            $this->success('删除成功','subject/index');
        }$this->error('删除失败');


    }
}
